import { useTenantId } from '@hooks/useRootWindow';
/* hooks */
import { 
  useFetchSettingGPTRobotAppAdd, 
  useFetchSettingGPTRobotAppDelete, 
  useFetchSettingGPTRobotAppList, 
  useFetchSettingGPTRobotAppListWithSetting, 
  useFetchSettingGPTRobotAppRangeInsert, 
  useFetchSettingGPTRobotLogCount,
  useFetchSettingGPTRobotUpdateSettingModelName,
  useFetchSettingGPTRobotUpdateSettingShare,
  useInitXiaoBaoChat
} from '@gpt/hooks/robot';
/* vue */
import { computed, Ref, ref } from 'vue'
/* utils */
import { confirm } from '@src/util/platform';
/* type */
import { isFalsy } from '@src/util/type';
import { useFetchSettingGPTSystemRobot } from '@gpt/hooks/robot';
import { DeleteGPTRobotAppServiceModel, GetGPTRobotAppListModel } from '@model/param/in/AI';
import { getRootWindowInitData } from 'pub-bbx-utils';

function useSettingGPTRobotHome(isShare?: Ref<boolean>) {
  
  const {
    initChat
  } = useInitXiaoBaoChat()
  
  const {
    loading: fetchGptRobotLoading,
    fetchGptSystemRobot,
    gptSystemRobot
  } = useFetchSettingGPTSystemRobot()
  
  const {
    loading: fetchGptRobotAppListLoading,
    fetchGptRobotAppListWithSetting: fetchGptRobotAppList,
    gptRobotAppListWithSetting: gptRobotAppList,
    chatRobotAppList
  } = useFetchSettingGPTRobotAppListWithSetting(isShare)
  
  const {
    loading: fetchGptRobotLogCountLoading,
    fetchGptRobotLogCount,
    countList,
    isSuccess: fetchGptRobotLogCountSuccess
  } = useFetchSettingGPTRobotLogCount()
  
  const {
    loading: fetchGptRobotAppAddLoading,
    fetchGptRobotAppAdd,
    isSuccess: fetchGptRobotAppAddSuccess
  } = useFetchSettingGPTRobotAppAdd()
  
  const {
    loading: fetchGptRobotAppDeleteLoading,
    fetchGptRobotAppDelete,
    isSuccess: fetchGptRobotAppDeleteSuccess
  } = useFetchSettingGPTRobotAppDelete()
  
  const {
    loading: fetchUpdateGptRobotSettingShareLoading,
    fetchUpdateGptRobotSettingShare,
    isSuccess: fetchUpdateGptRobotSettingShareSuccess
  } = useFetchSettingGPTRobotUpdateSettingShare()
  
  const loading = computed(() => {
    return (
      fetchGptRobotLoading.value 
      || fetchUpdateGptRobotSettingShareLoading.value
      || fetchGptRobotAppListLoading.value
      || fetchGptRobotAppAddLoading.value
      || fetchGptRobotAppDeleteLoading.value
      || fetchGptRobotLogCountLoading.value
    )
  })
  
  const initialize = async () => {
    onFetchGptRobotLogCountHandler()
  }
  
  const onfetchGptRobotAppListHandler = (robotId?: string, tenantId?: string) => {
    
    let params: GetGPTRobotAppListModel = {
      robotId: robotId || gptSystemRobot.value.id as unknown as number
    }
    
    if (isShare?.value) {
      params = Object.assign(params, { tenantId })
    }
    
    return fetchGptRobotAppList(params)
  }
  
  const onFetchGptRobotLogCountHandler = () => {
    const params = {
      robotId: gptSystemRobot.value.id as unknown as number
    }
    return fetchGptRobotLogCount(params)
  }
  
  const onUpdateRobotSettingShareHandler = (share: boolean) => {
    
    const params = {
      id: gptSystemRobot.value.id as unknown as number,
      share: share
    }
    
    return fetchUpdateGptRobotSettingShare(params).then(() => {
      
      if (fetchUpdateGptRobotSettingShareSuccess.value) {
        fetchGptSystemRobot()
      }
      
      return fetchUpdateGptRobotSettingShareSuccess.value
    })
    
  }
  
  const onDelRobotAppServiceHandler = async (params: DeleteGPTRobotAppServiceModel) => {
    
    const confirmed = await confirm('确认删除？')
    if (isFalsy(confirmed)) {
      return
    }
    
    return fetchGptRobotAppDelete(params).then(() => {
      
      if (fetchGptRobotAppDeleteSuccess.value) {
        onfetchGptRobotAppListHandler()
        initChat()
      }
      
      return fetchGptRobotAppDeleteSuccess.value
    })
  }
  
  return {
    initChat,
    initialize,
    loading,
    countList,
    gptSystemRobot,
    gptRobotAppList,
    chatRobotAppList,
    onUpdateRobotSettingShareHandler,
    fetchGptRobotAppAdd,
    onfetchGptRobotAppListHandler,
    onDelRobotAppServiceHandler
  }
}

export {
  useSettingGPTRobotHome,
  useFetchSettingGPTSystemRobot,
  useFetchSettingGPTRobotAppRangeInsert
}
